package com.rightsidetech.http.controller.admin.mall;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.MallLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.dto.log.CommodityMarketName;
import com.rightsidetech.mall.dto.log.request.MallLogPageReq;
import com.rightsidetech.mall.dto.log.response.MallLogPageResp;
import com.rightsidetech.mall.dto.request.*;
import com.rightsidetech.mall.dto.response.CommodityDetailResp;
import com.rightsidetech.mall.dto.response.CommodityPageResp;
import com.rightsidetech.mall.dto.response.CommoditySearchResp;
import com.rightsidetech.mall.dto.response.CommoditySpescResp;
import com.rightsidetech.mall.entity.MallCommodityWithBLOBs;
import com.rightsidetech.mall.service.MallCommodityLogService;
import com.rightsidetech.mall.service.MallCommodityService;
import com.rightsidetech.system.service.AgentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/admin/mall_commodity/")
@ShenyuSpringMvcClient(path = "/admin/mall_commodity/**")
@Api(tags = "Admin: 商品相关")
public class MallCommodityController extends BaseController {

    @Resource
    private MallCommodityService mallCommodityService;
    @Resource
    private AgentInfoService agentInfoService;
    @Resource
    private MallCommodityLogService mallCommodityLogService;

    @MallLog(modifyType = ModifyName.SAVE,marketName = CommodityMarketName.BATTERY_MALL)
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/save_commodity")
    @ApiOperation("添加商品")
    public ResponseData saveCommodity(@RequestBody CommoditySaveReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if (null == loginInfoId || null == loginName) {
            return ResponseData.fail("未找到登录人");
        }
        return mallCommodityService.saveCommodity(req,loginName,loginInfoId);
    }


    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @GetMapping("/find_detail")
    @ApiOperation("查看商品详情")
    public ResponseData<CommodityDetailResp> findDetailResp(@RequestParam(value = "id",required = true) Long id){
         return mallCommodityService.findDetailById(id);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @GetMapping("/page")
    @ApiOperation("分页查询商品列表")
    public ResponseData<PageInfo<CommodityPageResp>> selectPage(CommodityPageReq req){
       return mallCommodityService.selectPageList(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @GetMapping("/list")
    @ApiOperation("不分页商品列表")
    @ApiOperationSupport(ignoreParameters = {"pageNo", "pageSize"})
    public ResponseData<List<CommodityPageResp>> selectList(CommodityPageReq req){
        return mallCommodityService.selectList(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/update")
    @ApiOperation("修改商品")
    @MallLog(modifyType = ModifyName.UPDATE,marketName = CommodityMarketName.BATTERY_MALL)
    public ResponseData updateCom(@RequestBody CommodityUpReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if (null == loginInfoId || null == loginName) {
            return ResponseData.fail("未找到登录人");
        }
        return mallCommodityService.updateCommodity(req,loginName,loginInfoId);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/verify")
    @ApiOperation("审核商品")
    public ResponseData verify(@RequestBody VerifyCommodityReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if (null == loginInfoId || null == loginName) {
            return ResponseData.fail("未找到登录人");
        }
        return mallCommodityService.verifyCommodity(req,loginName,loginInfoId);
    }


    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/grounding")
    @ApiOperation("上下架商品")
    @MallLog(modifyType = ModifyName.GROUNDING,marketName = CommodityMarketName.BATTERY_MALL)
    public ResponseData grounding(@RequestBody CommodityIsGroundingReq req){
        return mallCommodityService.isGrounding(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/sort")
    @ApiOperation("商品排序")
    public ResponseData sortCommodity(@RequestBody CommoditySortReq req){
        return mallCommodityService.sort(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/arraignment")
    @ApiOperation("商品提审")
    public ResponseData arraignmentCommodity(@RequestBody DelReq req){
        return mallCommodityService.arraignment(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/popular")
    @ApiOperation("爆款商品")
    @MallLog(modifyType = ModifyName.POPULAR,marketName = CommodityMarketName.BATTERY_MALL)
    public ResponseData popular(@RequestBody CommodityPopularReq req){
        return mallCommodityService.popularCommodity(req);
    }


    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/platform")
    @ApiOperation("平台商品")
    public ResponseData platform(@RequestBody PlatformGroundingReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        MallCommodityWithBLOBs commodity = mallCommodityService.selectByPrimaryKey(req.getCommodityId());
        if(ObjectUtil.isEmpty(commodity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到需要操作的数据");
        }
        if(commodity.getCommodityStatus() == -1 || commodity.getCommodityStatus() == 0 || commodity.getCommodityStatus() == 1){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"商品状态不正确");
        }
        if(req.getStatus() == 1){
            List<Long> agentIds = agentInfoService.selectAllAgentId();
            req.setAgentIds(agentIds);
        }else {
            if(CollUtil.isEmpty(req.getAgentIds())){
                return mallCommodityService.deleteAgentByCommodity(commodity.getId());
            }
            if (new HashSet<>(req.getAgentIds()).size() < req.getAgentIds().size()) {
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"代理商id不可重复");
            }
        }
        return mallCommodityService.platformCommodity(commodity,req.getAgentIds());
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/log_page")
    @ApiOperation("分页查询商品操作日志相关")
    public ResponseData<PageInfo<MallLogPageResp>> logPage(MallLogPageReq req){
        return mallCommodityLogService.page(req);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/search")
    @ApiOperation("商品名称下拉选")
    public ResponseData<List<CommoditySearchResp>> searchCom(@RequestParam(required = false) String commodityName){
        return mallCommodityService.searchCommodity(commodityName);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/specs_list")
    @ApiOperation("获取商品下的规格和条码")
    public ResponseData<List<CommoditySpescResp>> specsList(DelReq req){
         return mallCommodityService.specsIntegral(req);
    }
}
